<?php
    chdir('../../');
    require('inc/header.php');
    
    define('ERROR_WRONG_SESSION', 1);
    define('ERROR_WRONG_USER', 2);
    define('ERROR_NO_USER', 3);
    define('ERROR_NO_GROUP', 4);
    define('ERROR_WRONG_RIGHT', 5);
    define('ERROR_NO_MODULE', 6);
    

    if(isset($_SESSION['user']['uid'])) {
        $uid = (int)$_SESSION['user']['uid'];
        $user = new AnyUser($uid);
        if($user->getUid() > 0) {
            $db = Database::getInstance();
            switch($_GET['action']) {
                case 'get':
                    switch($_GET['type']) {
                        case 'user':
                            $user = exist('user',(int)$_GET['uid']);
                            
                            $userRights = array();
                            $uRights = UserDAO::getRights($user->getUid(), true);
                            foreach($uRights as $mid => $lvl) {
                                $module = new Module((int)$mid);
                                $userRights[] = array('name'=>$module->name,'lvl'=>$lvl,'mid'=>$mid);
                            }
                            
                            $groupRights = array();
                            $gRights = GroupDAO::getRights($user->getGid());
                            foreach($gRights as $mid => $lvl) {
                                $module = new Module((int)$mid);
                                $groupRights[] = array('name'=>$module->name,'lvl'=>$lvl,'mid'=>$mid);
                            }
                            
                            die(json_encode(array(
                                'uid'       =>  $user->getUid(),
                                'rights'    =>  $userRights,
                                'gRights'   =>  $groupRights
                            )));
                        break;
                        case 'group':
                            $group = exist('group',(int)$_GET['gid']);
                            $rights = GroupDAO::getRights($group->getGid());
                            $fullRights = array();
                            foreach($rights as $mid => $lvl) {
                                $module = new Module((int)$mid);
                                $fullRights[] = array('name'=>$module->name,'lvl'=>$lvl, 'mid'=>$mid);
                            }
                            die(json_encode(array(
                                'gid'   =>  $group->getGid(),
                                'rights'=>  $fullRights
                            )));
                        break;
                    }
                break;
                case 'set':
                    switch($_GET['type']) {
                        case 'user':
                            $user = exist('user',(int)$_GET['uid']);
                            $module = new Module((int)$_GET['mid']);
                            if($module->getMid() < 1) {
                                error(ERROR_NO_MODULE);
                            }
                            if($module->addUserRight($user->getUid(), (int)$_GET['lvl'])) {
                                die(json_encode(array('error'=>0,'name'=>$module->name,'mid'=>$module->getMid(),'lvl'=>(int)$_GET['lvl'])));
                            } else {
                                error(ERROR_WRONG_RIGHT);
                            }
                        break;
                        case 'group':
                            $group = exist('group',(int)$_GET['gid']);
                            $module = new Module((int)$_GET['mid']);
                            if($module->getMid() < 1) {
                                error(ERROR_NO_MODULE);
                            }
                            if($module->addGroupRight($group->getGid(), (int)$_GET['lvl'])) {
                                die(json_encode(array('error'=>0,'name'=>$module->name,'mid'=>$module->getMid(),'lvl'=>(int)$_GET['lvl'])));
                            } else {
                                error(ERROR_WRONG_RIGHT);
                            }
                        break;
                    }
                break;
                case 'del':
                    switch($_GET['type']) {
                        case 'user':
                            $user = exist('user',(int)$_GET['uid']);
                            $module = new Module((int)$_GET['mid']);
                            if($module->getMid() < 1) {
                                error(ERROR_NO_MODULE);
                            }
                            if($module->delUserRight($user->getUid())) {
                                die(json_encode(array('error'=>0,'mid'=>$module->getMid())));    
                            } else {
                                error(ERROR_WRONG_RIGHT);
                            }
                        break;
                        case 'group':
                            $group = exist('group',(int)$_GET['gid']);
                            $module = new Module((int)$_GET['mid']);
                            if($module->getMid() < 1) {
                                error(ERROR_NO_MODULE);
                            }
                            if($module->delGroupRight($group->getGid())) {
                                die(json_encode(array('error'=>0,'mid'=>$module->getMid())));    
                            } else {
                                error(ERROR_WRONG_RIGHT);
                            }
                        break;
                    }
                break;
            }
        } else {
            error(ERROR_WRONG_USER);
        }
    } else {
        error(ERROR_WRONG_SESSION);
    }
    
    
    function error($error) {
        die(json_encode(array('error'=>$error)));
    }
    
    function exist($what, $id) {
        switch($what) {
            case 'user':
                $user = new AnyUser($id);
                if($user->getUid() > 0) {
                    return $user;
                } else {
                    error(ERROR_NO_USER);
                }
            break;
            case 'group':
                $group = new Group($id);
                if($group->getGid() > 0) {
                    return $group;
                } else {
                    error(ERROR_NO_GROUP);
                }
            break;
        }
    }
?>
